package practice2;

import utils.node.Node;
import utils.NodeUtils;

/**
 * @author Tara
 * @implNote 给定一个单向链表，如何反转链表？
 */
public class Pra1 {

    public static void main(String[] args) {
        Object[] arr = {2, 4, 7, 9, 23, 21};
        // 构造一个链表
        Node node = NodeUtils.constructNode(arr);
        // 打印链表轨迹
        NodeUtils.printSingleNode(node);
        // 反转链表
        Node node1 = reverseLinkedList(node);
        //打印链表结构
        NodeUtils.printSingleNode(node1);
    }

    //反转链表
    public static Node reverseLinkedList(Node head){
        Node preNode = null;
        Node nextNode = null;
        while (null!=head){
            nextNode = head.nextNode;
            head.nextNode = preNode;
            preNode = head;
            head = nextNode;
        }
        while (null!=head){
            System.out.print(head.value+" -> ");
        }
        return preNode;
    }

}
